/**   
 * @Description 类功能描述
 * @Author: lvcai
 * @Create Date 2013-11-18 下午10:21:56
 * @Modified By:lvcai
 * @Modified Date:2013-11-18 下午10:21:56
 * @version v1.0
 * @Copyright(c)2013:东吴软件技术有限公司  
 */
package com.apache.flow.taskHandler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Map;

import org.jbpm.api.JbpmException;
import org.jbpm.api.cmd.Environment;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.lob.BlobStrategy;
import org.jbpm.pvm.internal.lob.BlobStrategyBlob;
import org.jbpm.pvm.internal.lob.Lob;
import org.jbpm.pvm.internal.repository.DeploymentImpl;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.session.RepositorySession;
import org.jbpm.pvm.internal.util.IoUtil;

public class MyUpdateDeploymentResourceCmd extends AbstractCommand<Void> {

	private String deploymentId;
	private String resourceName;

	protected byte[] bytes;
	InputStream picStream;

	private static final long serialVersionUID = -5572609923178245386L;
	public static final int BUFFERSIZE = 4096;

	public MyUpdateDeploymentResourceCmd(String deploymentId, String resourceName, InputStream inputStream) {
		this.deploymentId = deploymentId;
		this.resourceName = resourceName;

		try {
			bytes = this.readBytes(inputStream);
		} catch (IOException e) {
			throw new JbpmException("could not read resource: " + resourceName, e);
		} finally {
			IoUtil.close(inputStream);
		}
	}

	//从jbpm源码中找来的代码 
	public byte[] readBytes(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		transfer(in, out);
		return out.toString().getBytes("UTF-8");
	}

	public static long transfer(InputStream in, OutputStream out) throws IOException {
		long total = 0;
		byte[] buffer = new byte[BUFFERSIZE];
		for (int count; (count = in.read(buffer)) != -1;) {
			out.write(buffer, 0, count);
			total += count;
		}
		return total;
	}

	@Override
	public Void execute(Environment env) throws Exception {
		RepositorySession repositorySession = env.get(RepositorySession.class);
		DeploymentImpl deployment = repositorySession.getDeployment(deploymentId);

		// 先找到旧的Lob对象
		Field lobMapField = deployment.getClass().getDeclaredField("resources");
		lobMapField.setAccessible(true);
		Map<String, Lob> resources = (Map<String, Lob>) lobMapField.get(deployment);
		Lob foundLob = resources.get(resourceName);

		if (foundLob == null) {
			throw new JbpmException("resource named \"" + resourceName + "\" hasn't found! ");
		}
		BlobStrategy blobStrategy = new BlobStrategyBlob();
		blobStrategy.set(bytes, foundLob);

		DbSession session = env.get(DbSession.class);
		session.update(foundLob);

		//	     RepositoryService repositoryService = env.get(RepositoryService.class);
		//	     RepositoryServiceImpl impl=(RepositoryServiceImpl) repositoryService;
		//	     impl.updateDeploymentResource(deploymentId, resourceName, picStream);
		return null;
	}

}
